﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DLL;

namespace BLL
{
    public static class cj_BLL
    {
        //创建数据对象
        private static dbExamDataContext db = new dbExamDataContext();

        /// <summary>
        /// 根据班级编号获取学生成绩
        /// </summary>
        /// <param name="bjNum">班级编号</param>
        public static object GetCJInfo(decimal bjNum)
        {
            object obj = new object();
            obj = from p in db.CJ where p.XS.bjNum == bjNum orderby p.xsId select new { p.XS.BJ.bjName, p.xsId, p.XS.xsName, p.KM.kmName, p.cj, p.date };
            return obj;
        }

        /// <summary>
        /// 根据学生姓名获取学生成绩
        /// </summary>
        /// <param name="xsName">学生姓名</param>
        public static object GetCJInfo(string xsName)
        {
            object obj = new object();
            obj = from p in db.CJ where p.XS.xsName == xsName orderby p.xsId select new { p.XS.BJ.bjName, p.xsId, p.XS.xsName, p.KM.kmName, p.cj, p.date };
            return obj;
        }

        /// <summary>
        /// 根据班级编号和学号获取成绩
        /// </summary>
        /// <param name="bjNum">班级编号</param>
        /// <param name="xsName">学生姓名</param>
        public static object GetCJInfo(decimal bjNum, string xsName)
        {
            object obj = new object();
            obj = from p in db.CJ where p.XS.xsName == xsName && p.XS.bjNum == bjNum orderby p.xsId select new { p.XS.BJ.bjName, p.xsId, p.XS.xsName, p.KM.kmName, p.cj, p.date };
            return obj;
        }

        /// <summary>
        /// 保存考试成绩
        /// </summary>
        /// <param name="newCJ">新的成绩单</param>
        public static bool SaveCJ(CJ newCJ)
        {
            bool bl = false;
            //验证相同数据是否已存在
            try
            {
                if (db.CJ.Count(p => p.kmNum == newCJ.kmNum && p.date == newCJ.date) > 0)
                    return false;
                db.CJ.InsertOnSubmit(newCJ);
                db.SubmitChanges();
                bl = true;
            }
            catch { }
            return bl;
        }
    }
}
